//
//  DayArrangeCell.swift
//  MyImGuider
//
//  Created by 王鹏宇 on 2018/9/20.
//  Copyright © 2018年 王鹏宇. All rights reserved.
//

import UIKit

class DayArrangeCell: UITableViewCell {
    
    @IBOutlet weak var cityView: UIView!
    @IBOutlet weak var cityNameLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var cityImageView: UIImageView!
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var kuaikuaiImageview: UIImageView!
    
    @IBOutlet weak var pageImageView: UIImageView!
    @IBOutlet weak var showMoreBtn: UIButton!
    
    @IBOutlet weak var pageHeight: NSLayoutConstraint!
    
    @IBOutlet weak var actionNameLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var introView: CustomTagView!
    @IBOutlet weak var shadowView: UIView!
    
    
//    var isHideCityView : Bool = true {
//        
//        didSet{
//            cityView.isHidden = isHideCityView
//            kuaikuaiImageview.isHidden = isHideCityView
//        }
//    }
    
    var showMoreImages:((UIView)->Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.cityImageView.layer.cornerRadius = 25
        self.cityImageView.clipsToBounds = true
        
        self.bgView.layer.cornerRadius = 8
        self.shadowView.layer.cornerRadius = 8
        self.shadowView.clipsToBounds = true
        self.bgView.clipsToBounds = true
        
        self.contentView.backgroundColor =  kThemeBgColor
        pageImageView.isHidden = true
        pageHeight.constant = 0
        self.selectionStyle = .none
        self.showMoreBtn.addShadow(type: .btn)
        self.shadowView.addShadow(type: .dayArrange)
        self.timeLabel.text = ""
    }
    
    var city:City? {
        
        didSet {
            
            cityNameLabel.text = city?.cityName
            
            cityImageView.setImage(string: city?.pic, placeholder: UIImage(named:"placeholder_squ")!)
//            if let datestr = city?.destinationDate,let date = Date.date(datestr, "yyyy-MM-dd") {
//
//                dateLabel.text = date.string("MM月dd日")
//            } else {
//
//                dateLabel.text = ""
//            }
        }
    }
    
    var day:Day? {
        
        didSet {
            
            if let pics = day?.pictureArray, pics.count > 0 {
                
                self.pageImageView.setImage(string: pics.first, placeholder:UIImage(named:"placeholder_rec")!)
                setIsHasImage(sender: true)
            } else {
                
                setIsHasImage(sender: false)
            }
            
            self.actionNameLabel.text = "Day \(day?.day ?? 0)"
            
            
            
            if let array = day?.eventInfo {
                
                introView.setTagArray(array) { maker in
                    
                    maker?.fillColor = kThemeBgColor
                    maker?.insets =  UIEdgeInsets(top: -5, left: -10, bottom: -5, right: -10);
                    maker?.font = kFont12;
                    maker?.textColor = UIColor.black
                    maker?.linespace = 20;
                    maker?.space = 5;
                    maker?.hasNextImage = true;
                    maker?.maxWidth = CGFloat(kScreenWidth) - (50 + 16 * 5 );
                    maker?.tagAlignment = .left;
                    
                }
            } else {
                
                introView.tagViewH?.constant = 0
            }
            
            if let datestr = day?.date,let date = Date.date(datestr, "yyyy-MM-dd") {
                
                var dateText = date.string("MM月dd日")
                
                if AppManager.share.language == .english {
                    
                    dateText = date.string("dd MMM")
                }
                
//                timeLabel.text = dateText
                dateLabel.text = dateText
            } else {
                
//                timeLabel.text = ""
                dateLabel.text = ""
            }
        }
    }
    
    func setIsHasImage(sender : Bool){
        
        if sender == false {
            
            pageHeight.constant = 0
            pageImageView.isHidden = true
            showMoreBtn.isHidden = true
        }else{
            
            pageHeight.constant = CGFloat((kScreenWidth - 82)/16 * 9)
            pageImageView.isHidden = false
            showMoreBtn.isHidden = false
        }
    }
    
    
    @IBAction func showMoreAction(_ sender: Any) {
        
        showMoreImages?(self.pageImageView)
    }
}

